package projetDames.graphique;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

import projetDames.jeu.Jeu;
import projetDames.composant.Plateau;

public class ZoneGraphique extends JPanel implements MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int x;
	private int y;
	private Jeu jeu;
	private Plateau p;

	public ZoneGraphique(Jeu j, Plateau p) {
		super();
		this.jeu = j;
		this.p = p;

		this.setBackground(Color.WHITE);
		this.addMouseListener(this);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		this.affichePlateauGraph(g);

	}

	public void update() {

		this.repaint();
	}

	public void afficheCase(Graphics g, Color c, int i, int j) {
		g.setColor(c);
		g.fillRect(i * 50, j * 50, 50, 50);
		g.setColor(Color.BLUE);
		g.drawString("(" + j + "," + i + ")", i * 50, 10 + j * 50);
	}

	// affichage du plateau REMPLI en mode graphique
	public void affichePlateauGraph(Graphics g) {
		afficheCasePlateau(g); // on affiche le plateau
		affichePionPlateau(g);// on affiche les pions dessus
	}

	public void afficheCasePlateau(Graphics g) {
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				if (i % 2 == 0) {
					if (j % 2 == 0) {
						afficheCase(g, Color.WHITE, i, j);
					} else {
						afficheCase(g, Color.BLACK, i, j);
					}
				} else {
					if (j % 2 == 0) {
						afficheCase(g, Color.BLACK, i, j);
					} else {
						afficheCase(g, Color.WHITE, i, j);
					}
				}
			}
		}

	}

	public void affichePionPlateau(Graphics g) {
		for (int j = 0; j < 10; j++) {
			for (int i = 0; i < 10; i++) {

				if (p.getTabPlateau()[i][j] != null) {
					p.getTabPlateau()[i][j].dessine(g, this, j * 50, i * 50);

				}
			}
		}
	}

	public void mouseClicked(MouseEvent e) {

	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {
		x = e.getX();
		y = e.getY();

		jeu.clic(x, y);

	}

	public void mouseReleased(MouseEvent e) {
		// this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}

	public void mouseDragged(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

}
